<?php

include_once 'Database.php';

class User extends Database {

	const VISITOR_INT = 1;

	const VISITOR_STRING = 'Visitor';

	const INVENTOR_INT = 2;

	const INVENTOR_STRING = 'Inventor';

	const ADMIN_INT = 3;

	const ADMIN_STRING = 'Admin';

	const USER_DEFAULT_PASSWORD = 'sysVentory';
	private $user_id;
	private $email;
	private $grant;
	private $deleted;

	function __construct($user_id, $email, $grant = 1) {
		parent::__construct();
		$this->user_id = intval($user_id);
		$this->email = $email;
		$this->grant = intval($grant);
		$this->deleted = false;
	}

	private function grantToString($grant) {
		switch($grant) {
			case User::VISITOR_INT :
				return User::VISITOR_STRING;
			case User::INVENTOR_INT :
				return User::INVENTOR_STRING;
			case User::ADMIN_INT :
				return User::ADMIN_STRING;
			default :
				return 'undefined';
		}
	}

	/**
	 *
	 * @return the $user_id NULL
	 */
	public function getUser_id() {
		if(!$this->deleted) {
			return $this->user_id;
		} else {
			return null;
		}
	}

	/**
	 *
	 * @return the $email NULL
	 */
	public function getEmail() {
		if(!$this->deleted) {
			return $this->email;
		} else {
			return null;
		}
	}

	/**
	 *
	 * @return the $grant NULL
	 */
	public function getGrant() {
		if(!$this->deleted) {
			return $this->grant;
		} else {
			return null;
		}
	}

	/**
	 *
	 * @return string NULL
	 */
	public function getUserHtmlTable() {
		if(!$this->deleted) {
			$row = '<tr><td class="user_id">' . $this->user_id . '</td><td class="email">' . $this->email . '</td><td class="grant">' . $this->grantToString($this->grant) . '</td>';
			return $row;
		} else {
			return '';
		}
	}

	/**
	 *
	 * @param string $newPassword        	
	 * @return boolean
	 */
	public function setPassword($oldPassword, $newPassword) {
		if(!$this->deleted && is_string($oldPassword) && is_string($newPassword)) {
			try {
				$querry = "UPDATE `user` SET `password` = ? WHERE `id` = ? AND `password` = ?";
				$stmt = $this->db->prepare($querry);
				if($stmt) {
					$oldPassword = md5($oldPassword);
					$newPassword = md5($newPassword);
					
					$stmt->bind_param('sis', $newPassword, intval($this->user_id), $oldPassword);
					if($stmt->execute()) {
						return true;
					}
				}
			} catch(Exception $e) {
				return false;
			}
		}
		return false;
	}

	/**
	 *
	 * @return boolean
	 */
	public function resetPassword() {
		return $this->setPassword(User::USER_DEFAULT_PASSWORD);
	}

	/**
	 *
	 * @return boolean
	 */
	public function deleteUser() {
		try {
			$querry = "DELETE FROM `user` WHERE `id` = ?";
			$stmt = $this->db->prepare($querry);
			if($stmt) {
				$stmt->bind_param('i', $this->user_id);
				if($stmt->execute()) {
					$this->deleted = true;
					return true;
				}
				print_r($stmt);
			}
		} catch(Exception $e) {
			return false;
		}
		return false;
	}

	/**
	 *
	 * @param string $newEmail        	
	 * @return boolean
	 */
	public function setEmail($newEmail) {
		if(!$this->deleted && is_string($newEmail)) {
			try {
				$query = "UPDATE `user` SET `email` = ? WHERE `id` = ?";
				$stmt = $this->db->prepare($query);
				if($stmt) {
					$escapedNewEmail = mysql_real_escape_string($newEmail);
					
					$stmt->bind_param('si', $escapedNewEmail, $this->user_id);
					if($stmt->execute()) {
						$this->email = $newEmail;
						return true;
					}
				}
			} catch(Exception $e) {
				return false;
			}
		}
		return false;
	}

	/**
	 *
	 * @param integer $newGrant        	
	 * @return boolean
	 */
	public function setGrant($newGrant) {
		if(!$this->deleted) {
			try {
				$querry = "UPDATE `user` SET `fk_type` = ? WHERE `id` = ?";
				$stmt = $this->db->prepare($querry);
				if($stmt) {
					$newGrant = intval($newGrant);
					
					$stmt->bind_param('ii', $newGrant, $this->user_id);
					if($stmt->execute()) {
						$this->grant = $newGrant;
						return true;
					}
				}
			} catch(Exception $e) {
				return false;
			}
		}
		return false;
	}
}

?>